/*
Copyright 2011, CSE Shareware Foundation
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, 
are permitted provided that the following conditions are met:

 * Redistributions of source code must retain the above copyright notice, 
   this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright notice, 
   this list of conditions and the following disclaimer in the documentation 
   and/or other materials provided with the distribution.
 * Neither the name of CSE Shareware Foundation nor the names of its contributors 
   may be used to endorse or promote products derived from this software without 
   specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 
OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 
OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef _CSEOBJ_H_CSEAPI_
#define _CSEOBJ_H_CSEAPI_

// const of CSE type format specification
#define CSE_INT                    _T("i")
#define CSE_WINT                   _T("w")
#define CSE_FLOAT                  _T("f")
#define CSE_STR                    _T("s")
#define CSE_NSTR                   _T("S")
#define CSE_TIER                   _T("()")
#define CSE_BUFF                   _T("[]")
#define CSE_DICT                   _T("{}")
#define CSE_INSTANCE               _T("<>")
#define CSE_ID                     _T("%#")
#define CSE_NOTATOR                _T("%@")
#define CSE_STRONG                 _T("%+")
#define CSE_WEAK                   _T("%-")

class CSE_IMPEXP_IN TCseObj
{
private:
    TCseObj(TInt disable);          // disable construct by TInt
    TCseObj(TWint disable);         // disable construct by TWint
    TCseObj(TDouble disable);       // disable construct by TDouble
    TCseObj(const TXchar *disable); // disable construct by TXchar*

//    TBool operator==(CseObj obj) const;  // check confusion convertion
//    TBool operator!=(CseObj obj) const;  // check confusion convertion
//    TBool operator>(CseObj obj) const;   // check confusion convertion
//    TBool operator>=(CseObj obj) const;  // check confusion convertion
//    TBool operator<(CseObj obj) const;   // check confusion convertion
//    TBool operator<=(CseObj obj) const;  // check confusion convertion

protected:
    CseObj m_data;
    
    TCseObj& assign(CseObj obj);  // special assign, maybe override by deliver

public:
    TCseObj();
    TCseObj(CseObj obj);
    TCseObj(const TCseObj& obj);
    ~TCseObj();
    
    static TCseObj format(const TXchar *frmt, ...);

    // create by copy
    TCseObj(const TIntObj& iObj);
    TCseObj(const TWintObj& wObj);
    TCseObj(const TFloatObj& fObj);
    TCseObj(const TCharArray& s);
    TCseObj(const TIntArray& t);
    TCseObj(const TObjArray& b);
    TCseObj(const TEntryArray& d);
    
    // create by cut
    TCseObj(int MagicNum, TCharArray& s);
    TCseObj(int MagicNum, TIntArray& t);
    TCseObj(int MagicNum, TObjArray& b);
    TCseObj(int MagicNum, TEntryArray& d);

    // object assign
    TCseObj& operator=(const TCseObj& obj);
    TCseObj& operator=(CseObj obj);

    TBool operator==(TCseObj& obj) const;
    TBool operator!=(TCseObj& obj) const;
    TBool operator>(TCseObj& obj) const;
    TBool operator>=(TCseObj& obj) const;
    TBool operator<(TCseObj& obj) const;
    TBool operator<=(TCseObj& obj) const;

    operator CseObj() const;
    CseObj operator->() const;

    TIntObj& iObj() const;
    TWintObj& wObj() const;
    TFloatObj& fObj() const;
    TCharArray& sArray() const;
    TIntArray& tArray() const;
    TObjArray& bArray() const;
    TEntryArray& dArray() const;

    // str/tier/buff/dict
    int len() const;
    void clear() const;
    int fixSize(int iRsvd) const; // 0 for failed, other success
    void join(const TCseObj& obj, int index=0) const;

    // common API for str, tier, buff
    TCseObj slice(int index, int len=-1) const;
    const TCseObj& append(const TCseObj& obj) const;
    const TCseObj& insert(int index, const TCseObj& obj) const;
    void remove(int index, int len=1) const;
    int truncate(int iLen) const;
    void sort() const;

    // All CSE type
    int type() const;
    TCharArray str() const;
    TCharArray beStr() const;
    TUint32 hash() const;
    int compare(CseObj other) const; // 0: same, >0: large, <0: less

    CseObj transfer(const TCseObj& value);
    void exchange(TCseObj& other);
    TBool isMapped() const;
    TBool isLinked() const;

    TInt32 getFlag() const;
    void setFlag(TInt32 flag) const;
    TBool optionOn(int iBit) const;
    void setOption(int iBit) const;
    void clearOption(int iBit) const;

    // API for str
    TCseObj toId() const;

    // API for buff
    TCseObj& operator[](int index) const;
    TCseObj& sub(int index) const;
    TCseObj pop(int index=-1) const;
    int push(const TCseObj& obj) const;
    TCseObj toNotator(int index=0) const;

    // API for dict
    TBool isSpace() const;
    TCseObj get(const TCseObj& key) const;
    const TCseObj& set(const TCseObj& key, const TCseObj& value) const;
    TCseObj quickGet(const TCseObj& key) const;
    TBool quickSet(const TCseObj& key, const TCseObj& value) const;
    TCseObj getEx(const TCseObj& key, const TCseObj& vDefault) const;
    TCseObj setEx(const TCseObj& key, const TCseObj& value) const;
    TBool enumItem(PEntryPoint& pEntry, TCseObj *pKey=NULL, TCseObj *pValue=NULL) const;

    // API for strong
    TCseObj strongGet() const;
    const TCseObj& strongSet(const TCseObj& value) const;
};

extern CSE_IMPEXP_IN TCseObj CSE_HOLD(CseObj obj);

extern CSE_IMPEXP_IN int CsePrint();
extern CSE_IMPEXP_IN int CsePrint(const TCseObj& p0);
extern CSE_IMPEXP_IN int CsePrint(const TCseObj& p0, const TCseObj& p1);
extern CSE_IMPEXP_IN int CsePrint(const TCseObj& p0, const TCseObj& p1, const TCseObj& p2);
extern CSE_IMPEXP_IN int CsePrint(const TCseObj& p0, const TCseObj& p1, const TCseObj& p2, const TCseObj& p3);
extern CSE_IMPEXP_IN int CsePrint(const TCseObj& p0, const TCseObj& p1, const TCseObj& p2, const TCseObj& p3, const TCseObj& p4);
extern CSE_IMPEXP_IN int CsePrint(const TCseObj& p0, const TCseObj& p1, const TCseObj& p2, const TCseObj& p3, const TCseObj& p4, const TCseObj& p5);
extern CSE_IMPEXP_IN int CsePrint(const TCseObj& p0, const TCseObj& p1, const TCseObj& p2, const TCseObj& p3, const TCseObj& p4, const TCseObj& p5, const TCseObj& p6);
extern CSE_IMPEXP_IN int CsePrint(const TCseObj& p0, const TCseObj& p1, const TCseObj& p2, const TCseObj& p3, const TCseObj& p4, const TCseObj& p5, const TCseObj& p6, const TCseObj& p7);
extern CSE_IMPEXP_IN int CsePrint(const TCseObj& p0, const TCseObj& p1, const TCseObj& p2, const TCseObj& p3, const TCseObj& p4, const TCseObj& p5, const TCseObj& p6, const TCseObj& p7, const TCseObj& p8);
extern CSE_IMPEXP_IN int CsePrint(const TCseObj& p0, const TCseObj& p1, const TCseObj& p2, const TCseObj& p3, const TCseObj& p4, const TCseObj& p5, const TCseObj& p6, const TCseObj& p7, const TCseObj& p8, const TCseObj& p9);
extern CSE_IMPEXP_IN int CsePrint(const TCseObj& p0, const TCseObj& p1, const TCseObj& p2, const TCseObj& p3, const TCseObj& p4, const TCseObj& p5, const TCseObj& p6, const TCseObj& p7, const TCseObj& p8, const TCseObj& p9, const TCseObj& p10);
extern CSE_IMPEXP_IN int CsePrint(const TCseObj& p0, const TCseObj& p1, const TCseObj& p2, const TCseObj& p3, const TCseObj& p4, const TCseObj& p5, const TCseObj& p6, const TCseObj& p7, const TCseObj& p8, const TCseObj& p9, const TCseObj& p10, const TCseObj& p11);
extern CSE_IMPEXP_IN int CsePrint(const TCseObj& p0, const TCseObj& p1, const TCseObj& p2, const TCseObj& p3, const TCseObj& p4, const TCseObj& p5, const TCseObj& p6, const TCseObj& p7, const TCseObj& p8, const TCseObj& p9, const TCseObj& p10, const TCseObj& p11, const TCseObj& p12);
extern CSE_IMPEXP_IN int CsePrint(const TCseObj& p0, const TCseObj& p1, const TCseObj& p2, const TCseObj& p3, const TCseObj& p4, const TCseObj& p5, const TCseObj& p6, const TCseObj& p7, const TCseObj& p8, const TCseObj& p9, const TCseObj& p10, const TCseObj& p11, const TCseObj& p12, const TCseObj& p13);
extern CSE_IMPEXP_IN int CsePrint(const TCseObj& p0, const TCseObj& p1, const TCseObj& p2, const TCseObj& p3, const TCseObj& p4, const TCseObj& p5, const TCseObj& p6, const TCseObj& p7, const TCseObj& p8, const TCseObj& p9, const TCseObj& p10, const TCseObj& p11, const TCseObj& p12, const TCseObj& p13, const TCseObj& p14);
extern CSE_IMPEXP_IN int CsePrint(const TCseObj& p0, const TCseObj& p1, const TCseObj& p2, const TCseObj& p3, const TCseObj& p4, const TCseObj& p5, const TCseObj& p6, const TCseObj& p7, const TCseObj& p8, const TCseObj& p9, const TCseObj& p10, const TCseObj& p11, const TCseObj& p12, const TCseObj& p13, const TCseObj& p14, const TCseObj& p15);

#endif
